<script setup lang="ts">
//@ts-ignore
import Cookies from 'js-cookie'
import { ref, watch, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Menu as IconMenu, User, Setting } from '@element-plus/icons-vue'
import { ElBreadcrumb, ElBreadcrumbItem } from 'element-plus'

const route = useRoute()
const router = useRouter()
const isCollapse = ref(false)
const defaultOpeneds = ref<string[]>([])

// 根据路由设置默认展开的菜单
const setDefaultOpeneds = () => {
  const pathParts = route.path.split('/')
  if (pathParts.length > 2) {
    defaultOpeneds.value = [pathParts[2]]
  }
}

// 初始化菜单状态
setDefaultOpeneds()

// 监听路由变化
watch(() => route.path, () => {
  setDefaultOpeneds()
})

const handleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const userRole = computed(() => Cookies.get('user-role') || 'user')

const menuItems = computed(() => {
  const items = [
    { path: '/admin/dashboard', icon: IconMenu, title: '仪表盘', roles: ['admin'] },
    { path: '/admin/users', icon: User, title: '用户管理', roles: ['admin'] },
    { path: '/admin/settings', icon: Setting, title: '系统设置', roles: ['admin'] },
    { path: '/admin/roles', icon: User, title: '角色管理', roles: ['admin'] },
    { path: '/admin/counselors', icon: User, title: '咨询师管理', roles: ['admin'] },
    { path: '/admin/announcements', icon: User, title: '公告管理', roles: ['admin'] },
    { path: '/admin/appointments', icon: IconMenu, title: '预约管理', roles: ['admin', 'counselor', 'user'] },
    { path: '/admin/consultation-records', icon: IconMenu, title: '咨询记录', roles: ['admin', 'counselor', 'user'] },
    { path: '/admin/counselor-profile', icon: User, title: '咨询师信息管理', roles: ['counselor'] }
  ]

  return items.filter(item => item.roles.includes(userRole.value))
})

// 处理退出登录
const handleLogout = () => {
  // 清除 cookie 中的认证信息和用户角色
  Cookies.remove('api-token')
  Cookies.remove('user-role')
  // 跳转到登录页面
  router.push('/login')
}
</script>

<template>
  <el-container class="admin-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
      <div class="logo-container">
        <img src="/favicon.ico" alt="Logo" class="logo" />
        <span v-show="!isCollapse" class="title">管理系统</span>
      </div>
      <el-menu :collapse="isCollapse" class="el-menu-vertical" background-color="#304156" text-color="#bfcbd9"
        active-text-color="#409EFF" router :default-openeds="defaultOpeneds">
        <el-menu-item v-for="item in menuItems" :key="item.path" :index="item.path" :route="item.path">
          <el-icon>
            <component :is="item.icon" />
          </el-icon>
          <template #title>{{ item.title }}</template>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="header">
        <div class="header-left">
          <el-button type="primary" @click="handleCollapse">
            <el-icon>
              <IconMenu />
            </el-icon>
          </el-button>
          <el-breadcrumb class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>当前页面</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar size="small" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
              <span class="username">管理员</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主要内容区域 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.admin-container {
  height: 100vh;
}

.aside {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  background-color: #2b2f3a;
}

.logo {
  width: 32px;
  height: 32px;
}

.title {
  color: #fff;
  margin-left: 12px;
  font-size: 16px;
  font-weight: 600;
}

.el-menu-vertical {
  border-right: none;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}

.header-left {
  display: flex;
  align-items: center;
}

.breadcrumb {
  margin-left: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #606266;
}
</style>
